<?php
namespace app\job\controller;

use app\job\service\CronJobService;
use app\model\JobFailLogModel;
use think\App;
use think\facade\Log;
use think\queue\Job;
use think\swoole\Websocket;

/**
 * 消费者类
 * 用于处理队列中的任务
 */
class CronJob
{
    public int $timeout = 0;//防止超时
    public $mode;
    public $CronJobService;
    public function __construct(App $app)
    {
        $this->mode = new JobFailLogModel();
        $this->CronJobService = new CronJobService($app);
    }

    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array $data 发布任务时自定义的数据
     */
    public function fire(Job $job, array $data,Websocket $websocket): void
    {
        Log::channel('job')->error('执行方法：'.$data['method']??'空'.'执行时间：'.date('Y-m-d H:i:s'));
        $this->mode->startTrans();
        try {
            //执行业务处理
            switch ($data['method']) {
                case 'LoginLog':
                    //登录日志
                    if (!$this->CronJobService->LoginLog($data['data'],$websocket))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                case 'RatingScaleImport':
                    //导入量表
                    if (!$this->CronJobService->RatingScaleImport($data['data'],$data['channel']))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                case 'UserImport':
                    //导入用户
                    if (!$this->CronJobService->UserImport($data['data']))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                case 'generateReport':
                    //生成报告
                    if (!$this->CronJobService->generateReport($data['data'],$data['channel']))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                case 'generateWorkersReport':
                    //生成教职工报告
                    if (!$this->CronJobService->generateWorkersReport($data['data'],$data['channel']))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                case 'PushMessage':
                    //推送消息
                    if (!$this->CronJobService->PushMessage($data['data'],$websocket))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                case 'PlatformPushMessage':
                    //推送消息-区域
                    if (!$this->CronJobService->PlatformPushMessage($data['data'],$websocket))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                case 'personCharts':
                    //个人报告（单份）
                    if (!$this->CronJobService->personCharts($data['data']))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                case 'personChartsRecover':
                    //个人报告（单份）重置报告文件
                    if (!$this->CronJobService->personChartsRecover($data['data']))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                case 'evaluationLog':
                    //量表测评记录
                    if (!$this->CronJobService->evaluationLog($data['data']))
                        throw new \Exception($this->CronJobService->message.'line:'.$this->CronJobService->line);
                    break;
                default:
                    //参数错误
                    throw new \Exception('参数错误！');
            }
            $this->mode->commit();
            $job->delete();
        } catch (\Exception $e) {
            $this->mode->rollback();
            Log::channel('job')->error('错误信息：'.$e->getMessage().'执行时间：'.date('Y-m-d H:i:s'));
            //记录错误队列
            $this->mode->insert([
                'method' => $data['method'],
                'data' => json_encode($data,true),
                'message' => $e->getMessage(),
                'time' => time()
            ]);
            $job->delete();
        }
    }

}

